package com.zyy.smartcity1129;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.zyy.smartcity1129.all.AllFragment;
import com.zyy.smartcity1129.home.HomeFragment;
import com.zyy.smartcity1129.me.MeFragment;
import com.zyy.smartcity1129.news.NewsFragment;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.zhdj.ZhFragment;

public class MainActivity extends AppCompatActivity {

    private FrameLayout fragment;
    public static BottomNavigationView bottom;
    public static String mToken = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImQ3NjY0MGMxLWM3NTMtNGE5NS1hZDViLTM1M2ZkODQyMTFmYiJ9.BQa5hUOq5F6c0OtHuGXC5JdzjS2WoxDfPKOVmPlbxV1rrUcpOt_uVCSYdcW0TR7ptRd7PWQ8p5qd6XNe7SGA-Q";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Http.base = "http://124.93.196.45:10001";
        Http.token = mToken;
        //动态限权
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE}, 1);
        }
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,new HomeFragment()).commit();
        bottom.setOnNavigationItemSelectedListener(item->{
            switch (item.getItemId()){
                case R.id.home:
                    add(new HomeFragment());
                    break;
                case R.id.all:
                    add(new AllFragment());
                    break;
                case R.id.zh:
                    add(new ZhFragment());
                    break;
                case R.id.news:
                    add(new NewsFragment());
                    break;
                case R.id.me:
                    add(new MeFragment());
                    break;

            }
            return true;
        });
    }

    public void add(Fragment fragment){
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment,fragment).commit();

    }
    private void initView() {
        fragment = (FrameLayout) findViewById(R.id.fragment);
        bottom = (BottomNavigationView) findViewById(R.id.bottom);
    }
}